<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';


class dao_news_category {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT c2.cate_news_name AS parent_cat_name, c1.* FROM tbl_news_category c1 LEFT JOIN tbl_news_category c2 ON c1.parent_cat_news = c2.cat_news_id";
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function save($cat_news_id, $cate_news_name, $parent_cat_news) {
        $db = new connection();
        $con = $db->open_connect();

        if (empty($parent_cat_news)) {
            $parent_cat_news = "NULL";
        }
        
        if ($cat_news_id == 0) {
            $query = "INSERT INTO tbl_news_category(cate_news_name, parent_cat_news) VALUES (
                '" . $cate_news_name . "'," . $parent_cat_news . ")";
        } else {
            $query = "UPDATE tbl_news_category SET "
                    . "cate_news_name = '" . $cate_news_name . "',"
                    . "parent_cat_news = '" . $parent_cat_news . "' "
                    . "WHERE cat_news_id = " . $cat_news_id;
        }
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_news_category WHERE cat_news_id = " . $id;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_news_category WHERE cat_news_id = " . $id;
        /** @noinspection PhpParamsInspection */
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
    public function get_root_categories() {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT * FROM tbl_news_category WHERE parent_cat_news is NULL OR parent_cat_news = 0";
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }
    
    public function get_all_children_by_parent_id($parent_id) {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT * FROM tbl_news_category where parent_cat_news = ".$parent_id;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }
}
